package main

import (
	"fmt"
	"net/http"

	"golang.org/x/net/websocket"
)

func add(ws *websocket.Conn) {
	msg := make([]byte, 512)
	n, err := ws.Read(msg)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Receive: %s\n", msg[:n])

	sendMsg := "消息增加成功：" + string(msg[:n])

	_, err = ws.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Send: %s\n", sendMsg)
}

func main() {
	http.Handle("/add", websocket.Handler(add))
	err := http.ListenAndServe(":8888", nil)
	if err != nil {
		fmt.Println(err)
	}
}
